/**
 * 结算界面
 */
class AccountUI extends UIBase{
    // private lb_account:eui.Label;
    private lb_money:eui.Label;
    private btn_receive:eui.Label;
    private btn_double:eui.Group;
    private lb_double:eui.Label;
    // private lb_multiple:eui.Label;
    private receive_light:eui.Image;

    private money:number;

    private parma;

    constructor(data?)
    {
        super('AccountView', data)
    }

    public init()
    {
        this.money = 0;
        if(this.data['data'])
            this.parma = this.data['data'];

        this.Account();
        // this.btn_receive.touchEnabled = false;
        EffectUtil.instance().rotationEffect(this.receive_light, 2000);
    }

    //统计
    protected Account()
    {

        if(this.data.type == AccountType.NORMAL)
        {
            if(this.parma)
            {
                for(let v of this.parma)
                {
                    this.money += v['price'];
                }
            }
        }
        else if(this.data.type == AccountType.OFFLINE)
        {
            this.money = RoleCache.instance().offlineMoney;
            // this.lb_double.text = '离 线 收 益';
            
        }
        this.lb_double.text = '收益翻倍 x' + RoleCache.instance().getMoneyMultiple();
        // this.lb_money.text = '$'+this.money;
        EffectUtil.instance().NumChange(this.lb_money,0,this.money,500,'$');
        // egret.Tween.get(this.btn_receive).wait(1000).to({
        //     alpha:1
        // },1000).call(()=>{
        //     this.btn_receive.touchEnabled = true;
        // });
    }

    protected addEvent(){
        super.addEvent();
        // this.lb_multiple.text = 'X ' + RoleCache.instance().getMoneyMultiple();
        this.btn_receive.addEventListener(egret.TouchEvent.TOUCH_TAP,this.receiveMoney, this);
        this.btn_double.addEventListener(egret.TouchEvent.TOUCH_TAP,this.doubleHandle,this);
    }

    //直接领取金钱
    protected receiveMoney()
    {
        EffectUtil.instance().playBtnEffect(this.btn_receive);
        egret.Tween.get(this).wait(200).call(()=>{
			// 领取离线收益
            if(this.data.type == AccountType.OFFLINE)
            {   
                // UIManager.instance().showNormalLoading();
                SkillPrase.instance().getOfflineMoney(1);
            }
            // 领取钓鱼收益
            if(this.data.type == AccountType.NORMAL){
                let arrTemp = []
                for(let v of this.parma){
                    arrTemp.push(v['id']);
                }
                var arr = arrTemp.filter(function(x, index, self) {
                    return self.indexOf(x)===index;
                });
                
                SkillPrase.instance().getFishMoney(arr , this.money, 1);
            }
		});
    }

    public doubleHandle()
    {
        EffectUtil.instance().playBtnEffect(this.btn_double);
        egret.Tween.get(this).wait(200).call(()=>{
            if(this.data.type == AccountType.OFFLINE){ 
                let Success_call = function()
                {  
                    SkillPrase.instance().getOfflineMoney(RoleCache.instance().getMoneyMultiple());
                };
                let Error_call = function()
                {  
                    MessageUtil.tips('观看完整视频才能获得奖励哦')
                };
                WeChatgGameManager.instance().showVideoAd(GameConst.adList.offline_benefit, Success_call, Error_call, this);
            }
            else if(this.data.type == AccountType.NORMAL)
            {
                let Success_call = function()
                {
                    let arrTemp = []
                    for(let v of this.parma){
                        arrTemp.push(v['id']);
                    }
                    var arr = arrTemp.filter(function(x, index, self) {
                        return self.indexOf(x)===index;
                    });
                    SkillPrase.instance().getFishMoney(arr , this.money, RoleCache.instance().getMoneyMultiple());
                }
                let Error_call = function()
                {  
                    MessageUtil.tips('观看完整视频才能获得奖励哦')
                };
                WeChatgGameManager.instance().showVideoAd(GameConst.adList.account, Success_call, Error_call, this);
            }
        });
    }

    protected removeEvent(){ 
        super.removeEvent();
        this.btn_receive.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.receiveMoney,this);
        this.btn_double.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.doubleHandle,this);
    }

    public clear()
    {
        EffectUtil.instance().StopEffect(this.receive_light);
        let logic:MainUI = UIManager.instance().getLogicByName(UIConst.MAIN_UI) as MainUI;
        if(logic)
        {
            if(this.data.type == AccountType.NORMAL)
            {
                let NextTujian = TujianCache.instance().NextTujian;
                //有新图鉴的时候跳出新图鉴
                if(NextTujian)
                {
                    egret.setTimeout(()=>{
                        let data = {};
                        data['NextTujian'] = NextTujian;
                        data['isPop'] = true;
                        UIManager.instance().openUILogic(NewTujianUI, data);
                    }, this, 300);
                }
                logic.restartGame();
            }
            else
            {
                logic.touchChildren = true;
            }
        }
        
        this.money = null;
        this.parma = null;
        super.clear();
    }
}

enum AccountType{
    NORMAL,
    OFFLINE,
    MAX
}